<template>
  <div>
    <el-table :data="purchases">
      <el-table-column prop="buyNumber" label="采购编号"></el-table-column>
      <el-table-column prop="totalMoney" label="采购总金额(元)"></el-table-column>
      <el-table-column prop="totalNum" label="采购总数量"></el-table-column>
      <el-table-column prop="ptime" label="采购时间"></el-table-column>
      <el-table-column prop="user.uname" label="供应商"></el-table-column>
      <el-table-column label="采购单状态">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.isShipments==0&&scope.row.isAudit==2">未发货</el-tag>
          <el-tag type="success" v-if="scope.row.isShipments==1">已发货</el-tag>
          <el-tag type="danger" v-if="scope.row.isAudit==0">未审核</el-tag>
          <el-tag type="primary" v-if="scope.row.isAudit==2">未通过审核</el-tag>
          <el-tag type="success" v-if="scope.row.isAudit==1">审核通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column>
        <template slot="header" slot-scope="scope">
          <el-input
            v-model="purchase.buyNumber"
            @blur="search"
            placeholder="输入采购编号搜索"/>
        </template>
        <template slot-scope="scope">
          <el-link type="primary" @click="showDetail(scope.row.id)">查看详情</el-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageno"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <div>
      <show-details ref="showDetails"></show-details>
    </div>
  </div>

</template>

<script>
  import ShowDetails from "./ShowDetails";

  export default {
    name: "PurchaseView",
    data() {
      return {
        purchases: [],
        purchase: {buyNumber: ""},
        pageno: 1,
        pagesize: 5,
        total: 1
      }
    },
    methods: {
      getPurchases() {
        var params = new URLSearchParams();
        params.append("pageno", this.pageno);
        params.append("pagesize", this.pagesize);
        Object.keys(this.purchase).forEach(item => {
          params.append(item, this.purchase[item]);
        })
        this.$axios.get("purchase/purchase.action", {params: params})
          .then(res => {
            this.purchases = res.data.records;
            this.total = res.data.total;
          })
          .catch(err => {
            this.$message.error(err)
          })
      },
      search() {
        this.getPurchases();
      },
      showDetail(pid) {
        this.$refs.showDetails.dialogVisible = true;
        this.$refs.showDetails.purchaseDetail.pid = pid;
        this.$refs.showDetails.getData();
      },
      handleSizeChange(val) {
        //每页范围
        this.pagesize = val;
        this.getPurchases();
      },
      handleCurrentChange(val) {
        //当前页
        this.pageno = val;
        this.getPurchases();
      },
    },
    components: {
      ShowDetails
    },
    created() {
      this.getPurchases();
    }
  }
</script>

<style scoped>

</style>
